﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyAccount : System.Web.UI.Page
{
    BookStoreDataContext bs = new BookStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack) {
            var cardList =(from card_type in bs.card_types select card_type);
            drpCardType.DataSource = cardList;
            drpCardType.DataTextField = "name";
            drpCardType.DataValueField = "card_type_id";
            drpCardType.DataBind();
            bindDataToForm();
           
        }
        
       
    }
    public void bindDataToForm() {
        
        //bs = new BookStoreDataContext();
        
        var login = (from mem in bs.members
                     where
                      mem.member_login.Equals(Session["login"])

                     select mem);
        member currentUser = login.First();
        txtAddress.Text = currentUser.address;
        txtCardNum.Text = currentUser.card_number;
        txtEmail.Text = currentUser.email;
        txtFirstName.Text = currentUser.first_name;
        txtLastName.Text = currentUser.last_name;
        drpCardType.SelectedIndex = (currentUser.card_type_id == 1) ? 0 : 1;
    }
    public void changePasswordUI()
    {
        string userName = Session["login"].ToString();
        string oldPass = txtOldPass.Text.Trim();
        string newPass = txtNewPass.Text.Trim();
        string confirmPass = txtConfirmPass.Text.Trim();
        bool? isExist = false;
        if (bs.sp_isUserExist(userName, oldPass, ref isExist) <= 0)
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Wrong old password!";
        }
        else {
            if(bs.changePassword(Session["login"].ToString(),txtNewPass.Text.Trim())>0){
                lblResult.Visible = true;
                lblResult.Text = "Change password success";
            }
            
        }
    }
    protected void validateOldPassServer(ServerValidateEventArgs args)
    {
        string userName = Session["login"].ToString();
        string oldPass = txtOldPass.Text.Trim();
        string newPass = txtNewPass.Text.Trim();
        string confirmPass = txtConfirmPass.Text.Trim();
        bool? isExist = false;
        if (bs.sp_isUserExist(userName, oldPass, ref isExist) <= 0)
        {
            args.IsValid = false;
        }

        else
            args.IsValid = true;
    }
    protected void btnSavePass_Click(object sender, EventArgs e)
    {
        changePasswordUI();  
    }
    protected void txtOldPass_TextChanged(object sender, EventArgs e)
    {
        lblMessage.Visible = false;
    }
    protected void btnSaveInfo_Click(object sender, EventArgs e)
    {
        string login = Session["login"].ToString();
        string address = txtAddress.Text.Trim();
        int cardNum = int.Parse(txtCardNum.Text.Trim());
        int card_type_id = (drpCardType.SelectedIndex ==0)?1:2;
       
        int success = bs.updateMemberInfo(
            login,
            txtFirstName.Text.Trim(),
            txtLastName.Text.Trim(),
            txtEmail.Text.Trim(),
            address,
            card_type_id,
            cardNum
            );
        //if (success > 0)
        //{
        //    lblResult.Text = "Change info sucessful!";
        //}
        //else
        //{
        //    lblResult.Text = "Change info fail!";
        //}
        lblResult.Text = "Change information sucessful!";
        lblResult.Visible = true;
        
    }
}